<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/8
 * Time: 15:40
 */
class AdStatService extends CommonAction
{
    const EMPTY_PARAM = '-10000';
    const UV_EXISTS = '-10001';
    public $mad;
    public $mAdPlace;
    public function _initialize(){
        $this->mad = M('WapAd');
        $this->mAdPlace = M('WapAdPlace');
    }

    /**
     * 广告位图片的PV统计
     * @param int $adId 广告id
     * @return bool|mixed
     */
    public function statPV($adId)
    {

        if (!$adId) {
            return self::EMPTY_PARAM;
        }
        $object = str_replace(",","",$this->mad->where(array('ad_id' => $adId))->getField('object'));
        $sort = $this->mad->where(array('ad_id' => $adId))->getField('sort');
        $placeId = $this->mad->where(array('ad_id' => $adId))->getField('place_id');
        $accountId = $this->mAdPlace->where(array('place_id' => $placeId))->getField('account_id');
        $date = strtotime(date('Y-m-d'));
        $cacheKey = "ad_pv_{$adId}_{$date}_{$object}_{$sort}_{$placeId}_{$accountId}";
        $num = RedisService::incr($cacheKey);
        return $num;
    }

    /**
     * 图片的UV统计
     * @param int $adId     广告id
     * @param int $userId   广告用户id
     * @return mixed|string 用户UV统计
     */
    public function statUV($adId,$userId)
    {
        if(!$adId || !$userId) {
            return self::EMPTY_PARAM;
        }
        $object = str_replace(",","",$this->mad->where(array('ad_id' => $adId))->getField('object'));
        $sort = $this->mad->where(array('ad_id' => $adId))->getField('sort');
        $placeId = $this->mad->where(array('ad_id' => $adId))->getField('place_id');
        $accountId = $this->mAdPlace->where(array('place_id' => $placeId))->getField('account_id');
        $date = strtotime(date('Y-m-d'));
        $cacheKey = "ad_uv_{$adId}_{$userId}_{$date}_{$object}_{$sort}_{$placeId}_{$accountId}";
        $isExists = RedisService::keyExists($cacheKey);
        if (!$isExists) {
            //先去查数据表，如果数据表页不存在那么就执行计数
            return RedisService::incr($cacheKey);
            //否则就返回self::UV_EXISTS;
        } else {
            return self::UV_EXISTS;
        }
    }

    /**
     * 统计曝光数
     * @param $adId
     * @return mixed|string
     */
    public function statImgSwiperNum($data)
    {
        if (!$data) {
            return self::EMPTY_PARAM;
        }

        $date = strtotime(date('Y-m-d'));
        foreach($data as $key => $value){
            $object = str_replace(",","",$this->mad->where(array('ad_id' => $value[0]))->getField('object'));
            $sort = $this->mad->where(array('ad_id' => $value[0]))->getField('sort');
            $placeId = $this->mad->where(array('ad_id' => $value[0]))->getField('place_id');
            $accountId = $this->mAdPlace->where(array('place_id' => $placeId))->getField('account_id');
            $cacheKey = "ad_img_swiper_{$value[0]}_{$date}_{$object}_{$sort}_{$placeId}_{$accountId}";
            $result =  RedisService::incr($cacheKey,$value[1]);
            if(!$result){
                return false;
            }
        }
    }
}